package com.blog.controller;

import com.blog.Utils.Result;
import com.blog.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 任浩
 * @version 1.0
 *
 * ------文件上传附件类
 */

@RestController
@RequestMapping("/attachment")
public class AttachmentController {

    @Autowired
    private AttachmentService attachmentService;

    /**
     * 上传图片的方法
     * @return
     */
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {

        String url = attachmentService.uploadImage(file);
        return Result.success(url);
    }

    /**
     * 这是断点续传，分片上传视频的方法。
     * @param file
     * @param bucketName
     * @param shardingIndex
     * @return
     */
    @PostMapping("uploadVideo")
    public Result uploadVideo(@RequestParam("file") MultipartFile file,
                              @RequestParam("fileName") String bucketName,
                              @RequestParam("shardingIndex") Integer shardingIndex) {

        return attachmentService.uploadVideo(file,bucketName,shardingIndex);
    }


    /**
     * 分片上传之前，判断该文件上传到第几块分片了。
     * @param fileName
     * @return
     */
    @GetMapping("/judgeVideo")
    public Result judgeVideo(@RequestParam("fileName") String fileName) {

        return attachmentService.judgeVideo(fileName);
    }


    /**
     * 合并分片上传
     * @param fileName
     * @param shardingTotal
     * @return
     */
    @PostMapping("/mergeVideo")
    public Result mergeVideo(@RequestParam("fileName") String fileName,
                             @RequestParam("shardingTotal") Integer shardingTotal){

        return attachmentService.mergeVideo(fileName,shardingTotal);

    }

}
